<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">客户管理</a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input type="text" name="kw"  placeholder="请输入学校" autocomplete="off" class="layui-input" lay-verify="required">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock>
        <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="x_admin_show('添加客户','./member-add.html')"><i class="layui-icon"></i>添加</button>
        <span class="x-right" style="line-height:40px" id="schlen"></span>
      </xblock>
      <table class="layui-table">
        <thead>
          <tr>
            <th>
              <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th>序号</th>
            <th>销售负责人</th>
            <th>市/县</th>
            <th>客户名称</th>
            <th>客户类型</th>
            <th>地址</th>
            <th>特色专业</th>
            <th>全部专业</th>
            <th>联系人</th>
            <th colspan="2">操作</th></tr>
        </thead>
        <tbody id="schset">
        </tbody>
      </table>
      <!-- 分页开始 -->
		<div class="page">
		</div>
	<!-- 分页结束 -->
	</div>
    <script>
    var key='';
    layui.use(['form','layer'], function(){
      $ = layui.jquery;
      var form = layui.form,
      layer = layui.layer;
      form.on('submit(sreach)', function(data){
    	  key=data.field;
    	  $.post('school_list?currPage=1',data.field,function(result,status){
    		  if(status=='success'){
    			  var arr=JSON.parse(result);
    			  console.log(arr);
    			  render(arr);
    		  }
    	  });
    	  return false;
      });
    });
    $(document).ready(function(){
    	$.post('school_list?currPage=1',function(data,status){
    		if(status=='success'){
    		var objarr=JSON.parse(data);
    		render(objarr);
    		}else{
    			alert(status+':请稍后再试');
    		}
    	});	
    });
    
    //渲染数据
    function render(data){
    	var objarr=data;
    	var arr=data.list;
    	//获取总条数
    	$('#schlen').html('共有数据:'+objarr.totalCount+'条');
    	//移除上次渲染的数据
    	$('#schset').children().remove();
    	$('.page').children().remove();
    	//动态插入行
    	for(var i=0;i<arr.length;i++){
			$('#schset').append('<tr>'+
	            '<td>'+
	              '<div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='+arr[i].sch_id+'><i class="layui-icon">&#xe605;</i></div>'+
	            '</td>'+
	            '<td>'+(i+1)+'</td>'+
	            '<td>'+arr[i].sch_manager+'</td>'+
	            '<td>'+arr[i].sch_add+'</td>'+
	            '<td>'+arr[i].sch_name+'</td>'+
	            '<td>'+arr[i].dict.dict_item_name+'</td>'+
	            '<td>'+arr[i].sch_address+'</td>'+
	            '<td>'+arr[i].sch_keysubjects+'</td>'+
	            '<td>'+arr[i].sch_allsubjects+'</td>'+
	            '<td>'+(arr[i].school_links.length!=0?'<span class="layui-btn layui-btn-danger layui-btn-mini" onclick="x_admin_show(\'查看\',\'links-list.html#sch_id='+arr[i].sch_id+'\')">查看</span>':'<span class="layui-btn layui-btn-danger layui-btn-mini" onclick="x_admin_show(\'添加\',\'links-add.html#sch_id='+arr[i].sch_id+'\')">添加</span>')+'</td>'+
	            '<td>'+
	              '<span class="layui-btn layui-btn-danger layui-btn-mini" onclick="x_admin_show(\'修改\',\'member-edit.html#uuid='+arr[i].sch_id+'\')">修改</span>'+
	            '</td><td>'+  
	              '<span class="layui-btn layui-btn-danger layui-btn-mini" onclick="member_del(this,\''+arr[i].sch_id+'\')">删除</span></td>'+
	          '</td></tr>');
			}
    	//------------分页开始-----------------
		//判断是否出现上一页，当前页=1则不出现上一页
		if(objarr.currPage-1>0){$('.page').append('<a onclick="changePB('+(objarr.currPage-1)+')" style="cursor: pointer;">上一页</a>');}
    	//显示数字页码
		for(var i=(objarr.currPage-5>0?objarr.currPage-5:1);i<=(objarr.currPage+4<objarr.totalPage?objarr.currPage+4:objarr.totalPage);i++){
			if(i==objarr.currPage){$('.page').append('<a style="background:#888;color:white">'+i+'</a>')}else{
				$('.page').append('<a onclick="changePB('+i+')" style="cursor: pointer">'+i+'</a>');
			}
		}
    	//判断是否是最后一页
		if(objarr.currPage<objarr.totalPage){$('.page').append('<a onclick="changePB('+(objarr.currPage+1)+')" style="cursor: pointer">下一页</a>')}
    	//------------分页结束------------------------
    }
    
    function changePB(currPage){
    	$.post('school_list?currPage='+currPage,key,function(data,status){
    		if(status=='success'){
    			var data=JSON.parse(data);
    			render(data);
    		}
    	});
    }
    
    function member_del(obj,id){
         layer.confirm('确认要删除吗？',function(index){
              //发异步删除数据
              $.ajax({
            	  url:'school_del',
            	  datatype:'json',
            	  data:{'uuid':id},
            	  success:function (result){
            		  $(obj).parents("tr").remove();
    	              layer.msg('已删除!',{icon:1,time:1000});
            	  },
            	  error:function(error){
    	              layer.msg('删除失败!',{icon:1,time:1000});
            	  }
              });
          }); 
      }

    </script>
  </body>
</html>